package com.sgcc.activiti.controller;

import com.sgcc.activiti.domain.OAnomalyInfo;
import com.sgcc.activiti.domain.vo.TotalFailuresNumberDto;
import com.sgcc.activiti.service.IOAnomalyInfoService;
import com.sgcc.activiti.service.IProcessService;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.web.page.TableDataInfo;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 监控系统异常Controller
 *
 * @author sgcc
 * @date 2021-03-22
 */
@RestController
@RequestMapping("/abnormal")
public class OAnomalyInfoController extends BaseController {
    @Autowired
    private IOAnomalyInfoService oAnomalyInfoService;
    @Autowired
    private IProcessService processService;

    /**
     * 查询监控系统异常列表
     */
    //@PreAuthorize(hasPermi = "activiti:abnormal:list")
    @Log(title = "查询列表", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(OAnomalyInfo oAnomalyInfo) {
        startPage();
        List<OAnomalyInfo> list = oAnomalyInfoService.selectOAnomalyInfoList(oAnomalyInfo);
        return getDataTable(list);
    }


    //@PreAuthorize(hasPermi = "activiti:abnormal:list")
    @GetMapping("/listProcess")
    @Log(title = "查询流程列表", businessType = BusinessType.OTHER)
    public TableDataInfo listProcess(OAnomalyInfo oAnomalyInfo) {
        startPage();
        List<OAnomalyInfo> list = oAnomalyInfoService.selectOAnomalyInfoProcessList(oAnomalyInfo);
        return getDataTable(list);
    }


    /**
     * 导出监控系统异常列表
     */
    //@PreAuthorize(hasPermi = "activiti:abnormal:export")
    @Log(title = "监控系统异常", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OAnomalyInfo oAnomalyInfo) throws IOException {
        List<OAnomalyInfo> list = oAnomalyInfoService.selectOAnomalyInfoList(oAnomalyInfo);
        ExcelUtil<OAnomalyInfo> util = new ExcelUtil<OAnomalyInfo>(OAnomalyInfo.class);
        util.exportExcel(response, list, "abnormal");
    }

    /**
     * 获取监控系统异常详细信息
     */
    //@PreAuthorize(hasPermi = "activiti:abnormal:query")
    @GetMapping(value = "/{id}")
    @Log(title = "查询异常详情", businessType = BusinessType.OTHER)
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(oAnomalyInfoService.selectOAnomalyInfoById(id));
    }


    //    //@PreAuthorize(hasPermi = "activiti:abnormal:query")
    @GetMapping(value = "/getCount")
    public AjaxResult getCount() {
        return AjaxResult.success(oAnomalyInfoService.getCount());
    }

    /**
     * 新增监控系统异常
     */
    //@PreAuthorize(hasPermi = "activiti:abnormal:add")
    @Log(title = "监控系统异常", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OAnomalyInfo oAnomalyInfo) {
        return toAjax(oAnomalyInfoService.insertOAnomalyInfo(oAnomalyInfo));
    }

    //    @Log(title = "监控系统异常", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @Log(title = "添加异常", businessType = BusinessType.INSERT)
    public AjaxResult addAdmin(@RequestBody OAnomalyInfo oAnomalyInfo) {
        return toAjax(oAnomalyInfoService.insertOAnomalyInfo(oAnomalyInfo));
    }

    //    @Log(title = "监控系统异常", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addProcess")
    @Log(title = "添加异常流程", businessType = BusinessType.INSERT)
    public AjaxResult addAdminProcess(@RequestBody OAnomalyInfo oAnomalyInfo) {
        return toAjax(oAnomalyInfoService.insertOAnomalyInfoProcess(oAnomalyInfo));
    }

    /**
     * 修改监控系统异常
     */
    //@PreAuthorize(hasPermi = "activiti:abnormal:edit")
    @Log(title = "监控系统异常", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OAnomalyInfo oAnomalyInfo) {
        return toAjax(oAnomalyInfoService.updateOAnomalyInfo(oAnomalyInfo));
    }

    /**
     * 删除监控系统异常
     */
    //@PreAuthorize(hasPermi = "activiti:abnormal:remove")
    @Log(title = "监控系统异常", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(oAnomalyInfoService.deleteOAnomalyInfoByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "提交系统监控异常", businessType = BusinessType.UPDATE)
    @PostMapping("/submitApply/{id}")
    @ResponseBody
    public AjaxResult submitApply(@PathVariable String id) {
        try {
            OAnomalyInfo obj = oAnomalyInfoService.selectOAnomalyInfoById(id);
            processService.submitApply(obj, "abnormal");
            oAnomalyInfoService.updateOAnomalyInfo(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return error("提交申请出错：" + e.getMessage());
        }
        return success();
    }

    /**
     * 运营看板总览异常监控
     *
     * @param orgNo
     * @param statisticalTime
     * @return
     * @author chensong
     * @version 2020-11-04
     */
    @PostMapping(value = "/exceptionMonitoring")
    public AjaxResult exceptionMonitoring(String orgNo, String statisticalTime) {
        try {
            Map<String, Object> condition = new HashMap<>();
            condition.put("orgNo", orgNo);
            condition.put("statisticalTime", statisticalTime);
//            List<ExceptionMonitoringDto> result = operationBoardService.exceptionMonitoring(condition);
            HashMap<String, Object> result = oAnomalyInfoService.exceptionMonitoring(condition);
            return AjaxResult.success("查询运营看板总览异常监控成功！", result);
        } catch (Exception e) {
            return AjaxResult.error(("查询运营看板总览异常监控失败！失败信息：" + e.getMessage()));
        }
    }


    /**
     * 异常监控当日累计失败数(1.下载;2.上传)
     *
     * @param orgNo
     * @param statisticalTime
     * @return
     */
    @PostMapping(value = "totalFailuresNumber")
    public AjaxResult totalFailuresNumber(String orgNo,
                                          String statisticalTime,
                                          String type) {
        try {
            Map<String, Object> condition = new HashMap<>();
            condition.put("orgNo", orgNo);
            condition.put("statisticalTime", statisticalTime);
            condition.put("type", type);
            List<TotalFailuresNumberDto> result = oAnomalyInfoService.totalFailuresNumber(condition);
            return AjaxResult.success(result);
        } catch (Exception e) {
            AjaxResult.error();
        }
        return AjaxResult.error();
    }

    /**
     * 获取图标异常信息列表
     */
    //@PreAuthorize(hasPermi = "activiti:abnormal:list")
    @GetMapping("/getExceptionDetailsList")
    public TableDataInfo getExceptionDetailsList(OAnomalyInfo oAnomalyInfo) {
        startPage();
        List<OAnomalyInfo> list = oAnomalyInfoService.getExceptionDetailsList(oAnomalyInfo);
        return getDataTable(list);
    }

}
